<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用reduce方法实现数组去重</title>
</head>
<body>
    <script>

    /* let arr = [9,4,9,8,4].reduce((pre, cur) => {
        return pre.includes(cur) ? pre : pre.concat(cur);
    }, []);
    console.log(arr); */

    // reduce方法
    var arr = [9,4,9,8,4];
    var res = arr.reduce(function(pre,cur){
        /* if(pre.indexOf(cur)>=0){
            return pre
        }else{
            return pre.concat(cur)
        } */
        // 三目运算
        return pre.indexOf(cur)>=0?pre:pre.concat(cur)
    },[]);
    console.log(res);

    // 通常for方法
    /* var arr = [9,4,9,8,4];
    var newArr = [];
    for(var i=0;i<arr.length;i++){
        if(newArr.indexOf(arr[i]) == -1){
            newArr.push(arr[i])
        }
    }
    console.log(newArr) */


    </script>
</body>
</html>